<template>
  <div class="container">
    <el-card class="cerd-box">
      <!-- 头部区域 -->
      <div class="header">
        <span class="fize">说明：目前支持学科和关键字条件筛选</span>
        <el-button type="success" icon="el-icon-edit" size="medium" @click="$router.push('new')">新增试题</el-button>
      </div>

      <!-- 选项区域 -->
      <el-form ref="form" :model="OptinsForm" label-width="80px">
        <el-row>
          <el-col :span="6">
            <el-form-item label="学科">
              <el-select v-model="OptinsForm.subjectID" placeholder="请选择" @change="nba" style="width: 100%">
                <el-option :label="item.label" :value="item.value" v-for="item in choiceList" :key="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="二级目录">
              <el-select v-model="OptinsForm.catalogID" placeholder="请选择" style="width: 100%">
                <el-option :label="item.label" :value="item.value" v-for="item in twolist" :key="item.label"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="标签">
              <el-select v-model="OptinsForm.tags" placeholder="请选择" style="width: 100%">
                <el-option :label="item.label" :value="item.value" v-for="item in bianqian" :key="item.label"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="关键字">
              <el-input placeholder="根据题干搜索" v-model="OptinsForm.keyword"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="6">
            <el-form-item label="试题类型">
              <el-select v-model="OptinsForm.questionType" placeholder="请选择" style="width: 100%">
                <el-option :label="item.label" :value="item.value" v-for="item in qus" :key="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="难度">
              <el-select v-model="OptinsForm.difficulty" placeholder="请选择" style="width: 100%">
                <el-option :label="item.label" :value="item.value" v-for="item in dif" :key="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="方向">
              <el-select v-model="OptinsForm.direction" placeholder="请选择" style="width: 100%">
                <el-option :label="item" :value="item" v-for="item in dir" :key="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="录入人">
              <el-select v-model="OptinsForm.creatorID" placeholder="请选择" style="width: 100%">
                <el-option label="超级管理员" value="OptinsForm.user"></el-option>
                <el-option label="录入管理员" value="OptinsForm.users"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="6">
            <el-form-item label="题目备注">
              <el-input v-model="OptinsForm.remarks"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="企业简称">
              <el-input v-model="OptinsForm.shortName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="城市">
              <el-select v-model="OptinsForm.province" @change="cityChange" style="width: 48%; margin-right: 2%">
                <el-option v-for="item in chenshi.provinces" :label="item" :value="item" :key="item"></el-option>
              </el-select>
              <!-- 地区 -->
              <el-select v-model="OptinsForm.city" style="width: 50%">
                <el-option v-for="item in chenshi.cityDate" :label="item" :value="item" :key="item"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item style="text-align: right">
              <el-button @click="clear()">清除</el-button>
              <el-button @click="filter()" type="primary">搜索</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <!-- tab栏区域 -->
      <el-tabs v-model="cba" type="card" @tab-click="changeTab">
        <el-tab-pane label="全部" name="all"> </el-tab-pane>
        <el-tab-pane label="待审核" name="0"></el-tab-pane>
        <el-tab-pane label="已审核" name="1"></el-tab-pane>
        <el-tab-pane label="已拒绝" name="2"></el-tab-pane>
        <!-- 数据记录 -->
        <el-alert :title="`数据一共 ${total} 条`" style="margin-bottom: 15px" type="info" class="alert" :closable="false" show-icon></el-alert>
        <el-table :data="questionsList" class="my-table">
          <el-table-column label="试题编号" prop="number" width="120px"></el-table-column>
          <el-table-column label="学科" prop="subject" width="120px"></el-table-column>
          <el-table-column label="目录" prop="catalog" width="120px"></el-table-column>
          <el-table-column label="题型" prop="questionType" width="120px">
            <template slot-scope="scope">
              <i v-if="scope.row.questionType == 1">单选</i>
              <i v-else-if="scope.row.questionType == 2">多选</i>
              <i v-else>简单</i>
            </template>
          </el-table-column>
          <el-table-column label="题干" prop="question" width="280px">
            <template slot-scope="scope">
              <div v-html="scope.row.question"></div>
            </template>
          </el-table-column>
          <el-table-column label="录入时间" width="180px">
            <template slot-scope="scope">
              {{ scope.row.addDate | parseTimeByString }}
            </template>
          </el-table-column>
          <el-table-column label="难度" prop="difficulty" width="50">
            <template slot-scope="scope">
              <i v-if="scope.row.difficulty == 1">简单</i>
              <i v-else-if="scope.row.difficulty == 2">一般</i>
              <i v-else>困难</i>
            </template>
          </el-table-column>
          <el-table-column label="录入人" prop="chkUser" width="120px"></el-table-column>
          <el-table-column label="审核状态" prop="chkState" width="120px">
            <template slot-scope="scope">
              <i v-if="scope.row.chkState == 0">待审核</i>
              <i v-else-if="scope.row.chkState == 1">已审核</i>
              <i v-else>已拒绝</i>
            </template>
          </el-table-column>
          <el-table-column label="审核意见" prop="chkRemarks" width="150px"> </el-table-column>
          <el-table-column label="审核人" prop="creator" width="120px"></el-table-column>
          <el-table-column label="发布状态" prop="publishState" width="150px">
            <template slot-scope="scope">
              <i v-if="scope.row.publishState == 0">已下架</i>
              <i v-else-if="scope.row.publishState == 1">已发布</i>
              <i v-else>发布</i>
            </template>
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="200px" align="center">
            <template slot-scope="scope">
              <el-button @click="openPreviewDialog(scope.row)" type="text" size="small">预览</el-button>
              <el-button type="text" size="small" :disabled="scope.row.chkState === 0 ? false : true" @click="shenheClick(scope.row)">审核</el-button>
              <el-button type="text" size="small" :disabled="scope.row.publishState == 1 ? true : false" @click="dataClick">修改</el-button>
              <el-button type="text" size="small" @click="putawayClick(scope.row)">
                <i v-if="scope.row.publishState == 0">上架</i>
                <i v-if="scope.row.publishState == 1">下架</i>
              </el-button>
              <el-button type="text" size="small" :disabled="scope.row.publishState === 1 ? true : false" @click="removeClick(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-tabs>

      <!-- 分页 -->
      <el-pagination class="pags" background :current-page="OptinsForm.pagenum" :page-sizes="[5, 10, 15, 20]" :page-size="100" layout="  prev, pager,next, sizes, jumper" :total="total"> </el-pagination>
    </el-card>
    <!-- 预览 -->
    <questions-preview ref="questionPreview" :data="questionInfo"></questions-preview>
  </div>
</template>

<script>
import { simple as simple1 } from "@/api/hmmm/subjects.js";
import { questionType, difficulty, direction } from "@/api/hmmm/constants.js";
import { choice, detail, choiceCheck, choicePublish, remove } from "@/api/hmmm/questions.js";
import { simple as simple2 } from "@/api/hmmm/directorys.js";
import { simple as simple3 } from "@/api/base/users.js";
import { simple as simple4 } from "@/api/hmmm/tags.js";
import { provinces, citys } from "@/api/hmmm/citys.js";
import QuestionsPreview from "../components/questions-preview";
export default {
  components: {
    QuestionsPreview,
  },
  data() {
    return {
      OptinsForm: {
        subjectID: null,
        difficulty: null,
        questionType: null,
        tags: null,
        province: null,
        city: null,
        keyword: null,
        remarks: null,
        shortName: null,
        direction: null,
        creatorID: null,
        catalogID: null,
        page: 1,
        pagesize: 5,
        chkState: null,
      },
      total: 0,
      choiceList: [], //学科列表数据
      questionsList: [], //tab栏页面
      twolist: [],
      qus: questionType,
      dif: difficulty,
      dir: direction,
      chenshi: {
        provinces: provinces(),
        cityDate: [],
      },
      addDialogVisible: false,
      yulan: [],
      tiku: {},
      bianqian: [],
      shenheisShow: false,
      putawayList: {
        publishState: null,
      },
      questionInfo: {},
      cba: "all",
    };
  },
  created() {
    this.getsturs();
    this.getQuestionslist();
  },
  methods: {
    // 切换tab
    changeTab() {
      if (this.cba == "all") {
        this.OptinsForm.chkState = null;
        this.getQuestionslist();
      } else {
        this.OptinsForm.chkState = this.cba;
        this.getQuestionslist();
      }
    },
    //  获取学科列表
    async getsturs() {
      try {
        const res = await simple1();
        this.choiceList = res.data;
        this.subjectNameId = res.data.value;
        //  console.log(res);
      } catch (err) {
        console.log(err);
      }
    },
    //获取二级目录列表
    async nba() {
      try {
        const res = await simple2({ subjectID: this.OptinsForm.subjectID });
        this.twolist = res.data;
        //  console.log(res.data);
        const { data } = await simple4({ subjectID: this.OptinsForm.subjectID });
        this.bianqian = data;
      } catch (err) {
        console.log(err);
      }
    },
    clear() {
      for (const key in this.OptinsForm) {
        if (key !== "page" && key !== "pagesize") this.OptinsForm[key] = null;
      }
    },
    // 搜索
    filter() {
      this.OptinsForm.page = 1;
      this.getQuestionslist();
    },
    // 获取精品题库列表
    async getQuestionslist() {
      try {
        const res = await choice(this.OptinsForm);
        this.total = res.data.counts;
        this.questionsList = res.data.items;
      } catch (err) {
        console.log(err);
      }
    },
    // 点击预览按钮
    openPreviewDialog(questionInfo) {
      this.questionInfo = questionInfo;
      this.$nextTick(() => {
        this.$refs.questionPreview.open();
      });
    },
    // 点击修改跳转页面
    dataClick() {
      this.$router.push("/questions/new");
    },
    // 点击上下架
    async putawayClick(row) {
      const confirmResult = await this.$confirm(`您确定${row.publishState == 1 ? "下架" : "上架"}这道题目吗?','提示 `, {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).catch((err) => err);
      if (confirmResult !== "confirm") return;
      if (row.publishState == 0) {
        row.publishState = 1;
        const res = await choicePublish(row);
        this.$message.success("下架成功");
        this.getQuestionslist();
      } else if (row.publishState == 1) {
        row.publishState = 0;
        const res = await choicePublish(row);
        this.$message.success("上架成功");
      }
    },
    //点击删除按钮
    async removeClick(row) {
      await this.$confirm("此操作将永久删除该题目, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      });
      await remove(row);
      this.$message.success("删除成功");
      this.getQuestionslist();
    },
    //
    cityChange(e) {
      this.chenshi.cityDate = citys(e);
    },
  },
};
</script>

<style scoped lang="scss">
body {
  padding: 0;
  margin: 0;
  font-size: 16px;
}
.header {
  display: flex;
  justify-content: space-between;
  margin-bottom: 15px;
}
.cerd-box {
  margin: 10px;
}
.fize {
  color: pink;
  font-size: 12px;
}
/* /deep/.el-input {
    width: 270px ;
 } */

v-deep .el-form-item__label {
  font-size: 14px;
  color: #606266;
}
.shuju {
  height: 34px;
  line-height: 34px;
  background-color: #f4f4f5;
  color: #909399;
  padding-left: 10px;
}
.obg {
  padding-left: 5px;
}
.pags {
  margin-left: 1097px;
}
v-deep .el-form-item__content {
  margin-left: 2px;
}
::v-deep.el-col-12 {
  .el-form-item__content {
    margin-left: 0;
  }
}
.el-table {
  font-size: 16px;
}
.hrou {
  margin-bottom: 15px;
  font-size: 16px;
}
.foot {
  font-size: 16px;
  margin-bottom: 15px;
}
.banner {
  height: 50px;
  border-top: 1px solid #000;
  border-bottom: 1px solid #000;
}
.radio {
  margin-bottom: 15px;
}
</style>
